package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.pojo.TPerson;
import com.dk.pojo.TRole;
import com.dk.service.TPersonServiceI;
import com.dk.service.TRoleServiceI;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("tPersonController")
public class TPersonController {

    //用户
    @Reference //引用dubbo服务接口，不能使用  @Autowired
    private TPersonServiceI tPersonServiceI;

    //角色
    @Reference
    private TRoleServiceI tRoleServiceI;

    @RequestMapping("listGet")
    public String listGet(){
        return "user/list";
    }

    //用户查询
    @RequestMapping("getCarAllInfo")
    @ResponseBody
    public List<TPerson> getAll(){
        List<TPerson> list = tPersonServiceI.getAll();
        return list;
    }

    //用户跳转增加页面
    @RequestMapping("insertInfo")
    public String insertInfo(Model model){
        //角色表
        List<TRole> roles = tRoleServiceI.getInfoServer();
        model.addAttribute("roles",roles);
        return "user/add";
    }

    //确认增加
    @RequestMapping("addById")
    @ResponseBody
    public  int addById(TPerson tPerson, @RequestParam("roleIds[]")Integer[] roleIds){
        tPersonServiceI.addById(tPerson,roleIds);
        return 1;
    }

    //回显
    @RequestMapping("updateInfo/{pid}")
    public String updateInfo(@PathVariable("pid")Integer pid, Model model){

        //查询该id 用户信息
        TPerson tPerson = tPersonServiceI.getUpdate(pid);
        model.addAttribute("u",tPerson);

        //查询全部角色
        List<TRole> roles = tRoleServiceI.getInfoServer();
        model.addAttribute("roles",roles);

        //根据用户id 去查询对应的的角色id
        List<TRole> rolesById = tRoleServiceI.getUpdateInfo(pid);
        model.addAttribute("rolesById",rolesById);
        return "user/update";
    }
    //确认修改
    @RequestMapping("updateById")
    @ResponseBody
    public int updateById(TPerson tPerson, @RequestParam("roleIds[]")Integer[] roleIds){
        tPersonServiceI.updateById(tPerson,roleIds);
        return 1;
    }

    //删除
    @RequestMapping("deleteById/{pid}")
    @ResponseBody
    public int deleteInfo(@PathVariable("pid")Integer pid){
        tPersonServiceI.deleteInfo(pid);
        return 1;
    }
}
